package com.dave.admin.config.auth.pojo;

import com.dave.admin.modular.boss.entity.Permission;
import com.dave.admin.modular.boss.entity.UserAdmin;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息 对接 Spring Boot Security
 * @author Dave
 */
public class BossUserDetails implements UserDetails {

    private final UserAdmin userAdmin;

    private final List<Permission> permissionList;

    public BossUserDetails(UserAdmin userAdmin, List<Permission> permissionList) {
        this.userAdmin = userAdmin;
        this.permissionList = permissionList;
    }

    /**
     * 权限列表
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return permissionList.stream()
                .filter(permission -> permission.getValue() != null)
                .map(permission -> new SimpleGrantedAuthority(permission.getValue()))
                .collect(Collectors.toList());
    }


    /**
     * 获取密码
     */
    @Override
    public String getPassword() {
        return userAdmin.getPassword();
    }

    /**
     * 获取登录名
     */
    @Override
    public String getUsername() {
        return userAdmin.getUsername();
    }

    /**
     * 账户是否没有过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否没有锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }


    /**
     * 密码是否没有过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     */
    @Override
    public boolean isEnabled() {
        return userAdmin.getStatus() == 1 ? true : false;
    }

    public UserAdmin getUserAdmin() {
        return userAdmin;
    }


    public List<Permission> getPermissionList() {
        return permissionList;
    }
}
